package com.yulongtian.handler;

import com.alibaba.fastjson.JSON;
import com.yulongtian.utils.R;
import com.yulongtian.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败
 *
 * @author yulongTian
 * @create 2022-10-30 21:06
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        //未认证
//        ResponseResult responseResult = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "用户认证失败请重新登录");
        String jsonString = JSON.toJSONString(R.error().data("data", "用户认证失败请重新登录"));
        //自定义处理异常
        WebUtils.renderString(response, jsonString);

    }
}
